import { Component } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { DomSanitizer, Title } from '@angular/platform-browser';
import { ApiService } from '@core/api.service';
import { ReuseTabService } from '@delon/abc';
import { LocalStorage } from '@core/local.storage';
import { UserAgentInfoService } from '@core/userAgentInfo.service';

@Component({
  selector: 'app-remotepage',
  templateUrl: './remotepage.component.html'
})

export class RemotepageComponent {
  contentHtml: any;
  startime: any;
  status: any = true;

  constructor(
    private activatedRoute: ActivatedRoute,
    private sanitizer: DomSanitizer,
    private apiService: ApiService,
    private ls: LocalStorage,
    private reuseTabService: ReuseTabService,
    private titleService: Title,
    private userAgentInfoService: UserAgentInfoService
  ) {
    this.activatedRoute.params.subscribe((params: Params) => {
      // 根据ID找URL
      const res = JSON.parse(this.ls.get('menuTreeData'));
      for (const item of res) {
        if (item.children) {
          for (const list of item.children) {
            if (list.children) {
              for (const data of list.children) {
                if (data.id == params.id) {
                  this.greatIframe(data);
                  break;
                }
              }
            } else {
              if (list.id == params.id) {
                this.greatIframe(list);
                break;
              }
            }
          }
        }
      }
    });
  }

  // 根据参数读取地址
  greatIframe(item: any) {
    this.reuseTabService.title = item.text;
    this.titleService.setTitle(item.text + ' - 后台管理');

    switch (this.ls.get('areaInfo')) {
      case 'KMBUR_KMNSEG':
      case 'WLMQBUR_HAMSEG':
        switch (item.place) {
          case 'jsp':
            // 详情页路由
            this.contentHtml = this.sanitizer.bypassSecurityTrustHtml('<iframe onload="if(document.getElementById(\'loading\') != null){document.getElementById(\'loading\').remove()}" src="/csmis' + item.link + '" frameborder="0" allowtransparency="true" style="width:100%;height:' + window.innerHeight + 'px;"></iframe>');
            break;
        }
        break;
      default: // 非昆明
        switch (item.id) {
          case 'ALAIN5001301': // 应用服务器
          case 'ALAIN5001302': // 数据库服务器
          case 'ALAIN600002003': // 交班日报
          case 'ALAIN401410001': // 报警管理
          case 'ALAIN300613': // 调度导入施工系统日计划
          case '30011': // 年表工区编制
          case '30012': // 年表车间审核
          case '30013': // 年表段审批
          // case '50031': // 角色权限维护
          case '90004': // 月计划统计
          case '120011': // 仓库管理
          case '120012': // 物资管理
          case '120021': // 入库管理
          case '120031': // 材料出库维护
          case '120032': // 材料出库审批
          case '120041': // 库存查看
            // to do
            break;
          default:
            // 详情页路由
            this.contentHtml = this.sanitizer.bypassSecurityTrustHtml('<iframe onload="if(document.getElementById(\'loading\') != null){document.getElementById(\'loading\').remove()}" src="/csmis' + item.link + '" frameborder="0" allowtransparency="true" style="width:100%;height:' + window.innerHeight + 'px;"></iframe>');
        }
    }

    this.startime = new Date().getTime();
    // 路由跳转后触发
    if (this.status) {
      this.status = false;
      // 记录行为
      this.apiService.streamPost(Math.random().toString(36).substr(2), item.id, item.text, this.startime, new Date().getTime(), this.userAgentInfoService.getOs(), this.userAgentInfoService.getBrowser(), '');
    }
    // 当页面关闭后触发
    window.onunload = () => {
      this.status = false;
      // 记录行为
      this.apiService.streamPost(Math.random().toString(36).substr(2), item.id, item.text, this.startime, new Date().getTime(), this.userAgentInfoService.getOs(), this.userAgentInfoService.getBrowser(), '');
    };
  }
}
